﻿#define _CRT_SECURE_NO_WARNINGS
#include"Contacts.h"
//初始化
void ContactsInit(struct Contacts *pc)
{
	assert(pc);
	//数组名放到sizeof表示整个数组大小
	pc->data = NULL;
	pc->size = pc->capacity = 0;
	loadContact(pc);
}

void ContactsCreate(struct Contacts *pc) 
{
	int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;
	struct PeoInfo *tmp = (struct PeoInfo *)realloc(pc->data,sizeof(struct PeoInfo) * newcapacity);
	if (tmp != NULL)
	{
		pc->capacity = newcapacity;
		pc->data = tmp;
		printf("增容成功\n");
	}
	else 
	{
		perror("ContactsCreate");
		return;
	}
}

void ContactsAdd(struct Contacts *pc)
{

	assert(pc);

	if (pc->size == pc->capacity)
	{
		//扩容
		ContactsCreate(pc);
	}
	printf("请输入姓名\n");
	scanf("%30s", pc->data[pc->size].name);

	printf("请输入年龄\n");
	scanf("%d", &pc->data[pc->size].age);

	printf("请输入姓别\n");
	scanf("%5s", &pc->data[pc->size].sex);

	printf("请输入电话\n");
	scanf("%12s", &pc->data[pc->size].tele);

	printf("请输入地址\n");
	scanf("%30s", &pc->data[pc->size].addr);

	pc->size++;
	printf("添加成功\n");
}
//显示通讯录
void Contactsshow(struct Contacts *pc) 
{
	assert(pc);
	//对齐
	printf("%15s\t%5s\t%8s\t%15s\t%30s\t\n\n", "姓名", "年龄", "性别", "电话", "住址");
	for (int i = 0; i < pc->size; i++) 
	{
		printf("%15s\t%5d\t%8s\t%15s\t%30s\t\n\n", 
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}

//搜索
int ContactsSearch(struct Contacts *pc, char *name)
{
	assert(pc && name);
	for (int i = 0; i < pc->size; i++)
	{
		if (strcmp(name, pc->data[i].name) == 0 )
		{
			
			return i;
		}
	}
	
	return -1;
	
}

//显示此人
void PeoInfoshow(struct Contacts *pc, int i)
{
	assert(pc);
	
	printf("姓名：%15s\t年龄：%5d\t性别：%8s\t电话：%15s\t地址：%30s\t\n",
		pc->data[i].name,
		pc->data[i].age,
		pc->data[i].sex,
		pc->data[i].tele,
		pc->data[i].addr);
}

//判断为空
bool ContactsIsEmpty(struct Contacts *con) 
{
	assert(con);
	return con->size == 0;
}

//修改
void PeoInfoModify(struct Contacts *pc, int i) 
{
	if (!ContactsIsEmpty(pc))
	{
		printf("请修改姓名\n");
		scanf("%30s", &pc->data[i].name);

		printf("请修改年龄\n");
		scanf("%d", &pc->data[i].age);

		printf("请修改姓别\n");
		scanf("%5s", &pc->data[i].sex);

		printf("请修改电话\n");
		scanf("%12s", &pc->data[i].tele);

		printf("请修改地址\n");
		scanf("%30s", &pc->data[i].addr);
	}
	
}

//删除
void PeoInfoDel(struct Contacts *pc, int pos) 
{
	assert(pc);

	int begin = pos;
	while (begin < pc->size) 
	{
		pc->data[begin] = pc->data[begin + 1];
		begin++;
	}
	
	pc->size--;
}
//清空通讯录
void DestroyContacts(struct Contacts *pc)
{
	assert(pc);
	pc->capacity = pc->size = 0;
	free(pc->data);
	pc->data = NULL;
}

//保存通讯录的信息到文件中去
void SaveContact(struct Contacts *pc) 
{
	//以二进制的方式把数据写入文件中
	FILE *fp = fopen("data.txt","wb");
	if (fp == NULL) 
	{
		perror("SaveContact::fopen");
		exit(-1);
	}
	//文件打开成功，以二进制的方式写文件
	for (int i = 0; i < pc->size; i++) 
	{
		fwrite(pc->data + i, sizeof(struct PeoInfo), 1, fp);
	}
	//关闭文件
	fclose(fp);
	fp = NULL;
}

//加载文件中的信息到通讯录中去
void loadContact(struct Contacts *pc)
{
	//以二进制的方式打开文件，把文件中的信息写入程序中去
	FILE *fp = fopen("data.txt", "rb");
	if (fp == NULL)
	{
		perror("SaveContact::fopen");
		exit(-1);
	}
	//文件打开成功，以二进制的方式写文件
	//临时变量存放文件中读取出来的信息
	struct PeoInfo tmp = { 0 };
	while (fread(&tmp,sizeof(struct PeoInfo),1,fp))
	{
		//考虑扩容
		if (pc->size == pc->capacity)
			ContactsCreate(pc);
		pc->data[pc->size++] = tmp;
	}
	//关闭文件
	fclose(fp);
	fp = NULL;
}